//
//  BaseTabBarController.m
//  Rongniu
//
//  Created by Apple on 16/2/22.
//  Copyright © 2016年 Fangwei. All rights reserved.
//

#import "BaseTabBarController.h"
#import "Common.h"
#import "BaseNavigatinController.h"


@interface BaseTabBarController (){

//    RongniuManager *manager ;

}


@end

@implementation BaseTabBarController


-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self removeButton];
    
}



-(void) removeButton{
    
    for (UIView *view in self.tabBar.subviews)
    {
        //从字符串生成一个类名
        Class class = NSClassFromString(@"UITabBarButton");
        
        //找出UITabBarButton，移除掉
        if ([view isKindOfClass:class])
        {
            [view removeFromSuperview];
        }
        
    }
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //1、创建viewCtrls
    [self createViewCtrls];
    
    //2、自定义uitabbar
    [self customTabBar];
  
}

-(void)createViewCtrls{
    UIStoryboard *homeStbd = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
    UIStoryboard *tradeStbd = [UIStoryboard storyboardWithName:@"Renderings" bundle:nil];
    UIStoryboard *informationStbd = [UIStoryboard storyboardWithName:@"E_Shop" bundle:nil];
    UIStoryboard *mineStbd = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];

    NSArray *viewCtrls = @[
                           [homeStbd instantiateInitialViewController],
                           [tradeStbd instantiateInitialViewController],
                           [informationStbd instantiateInitialViewController],
                           [mineStbd instantiateInitialViewController],

                           ];
    
    self.viewControllers = viewCtrls;
}


-(void) customTabBar{
    
    //背景
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"caidan"]];
    
    //按钮的宽度
    CGFloat buttonWidth = kScreenWidth/4.0;
    

    NSArray *imageoffArray= @[
                            @"shouye_off",
                            @"jiaoyi_off",
                            @"shixun_off",
                            @"wo_off",
                            ];


    NSArray *imageonArray = @[
                             @"shouye_on",
                             @"jiaoyi_on",
                             @"shixun_on",
                             @"wo_on",
                             ];
     
     for (int i = 0; i < imageoffArray.count; i++)
     {

     
     //每个按钮的frame
     CGRect frame = CGRectMake(buttonWidth * i, 0, buttonWidth, 49);
     UIButton *button = [[UIButton alloc]initWithFrame:frame];
    [button setImage:[UIImage imageNamed:imageoffArray[i]] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:imageonArray[i]] forState:UIControlStateSelected];
     button.tag = 100 + i;
     [button addTarget:self
     action:@selector(buttonAction:)
     forControlEvents:UIControlEventTouchUpInside];

     [self.tabBar addSubview:button];
     }
    
    //设置默认第一个
    UIButton *bt1 =   (UIButton *)[self.view viewWithTag:100];
    bt1.selected = YES;

    

    
    
}


-(void) presentLogin{
    
    
//    LoginViewController *loginCtrl = [[LoginViewController alloc]init];
//    
//    
//    UINavigationController *nav = [[BaseNavigatinController alloc] initWithRootViewController:loginCtrl];
//    [self presentViewController:nav animated:YES completion:NULL];
//    
    
}

//按钮的点击事件
- (void)buttonAction:(UIButton *)button {
    UIButton *bt1 =  (UIButton *)[self.view viewWithTag:100];
    UIButton *bt2 =  (UIButton *)[self.view viewWithTag:101];
    UIButton *bt3 =  (UIButton *)[self.view viewWithTag:102];
    UIButton *bt4 =  (UIButton *)[self.view viewWithTag:103];
    
    bt1.selected = NO;
    bt2.selected = NO;
    bt3.selected = NO;
    bt4.selected = NO;
    switch (button.tag-100) {
        case 0:
            bt1.selected = YES;
            break;
        case 1:
            bt2.selected = YES;
            break;
        case 2:
            bt3.selected = YES;
            break;
        case 3:
            bt4.selected = YES;
            break;
        default:
            break;
    }
    
    //切换控制器
    self.selectedIndex = button.tag-100;
    
    
    //    if (button.tag-100 == 3) {
    //
    //        if (manager.loginSucceed) {
    //
    //            self.selectedIndex = button.tag-100;
    //        }
    //        else{
    //
    //            [self presentLogin];
    //            return;
    //
    //        }
    //
    //    }else{
    //
    //        //切换控制器
    //        self.selectedIndex = button.tag-100;
    //
    //    
    //    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
